<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 持久化机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            padding: 20px;
            border-radius: 10px;
            font-family: 'Consolas', 'Monaco', monospace;
            position: relative;
            overflow-x: auto;
        }
        
        .code-block::before {
            content: 'Redis Config';
            position: absolute;
            top: 10px;
            right: 20px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-fadeInUp">
                Redis 持久化机制
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto animate-fadeInUp" style="animation-delay: 0.2s;">
                深入理解 RDB 快照与 AOF 日志，构建高可用的数据存储方案
            </p>
            <div class="mt-10 flex justify-center space-x-8 animate-fadeInUp" style="animation-delay: 0.4s;">
                <div class="text-center">
                    <i class="fas fa-database text-4xl mb-2"></i>
                    <p class="text-sm">数据持久化</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shield-alt text-4xl mb-2"></i>
                    <p class="text-sm">故障恢复</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-rocket text-4xl mb-2"></i>
                    <p class="text-sm">高性能存储</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 section-title">
                <i class="fas fa-info-circle mr-3 text-gradient"></i>
                核心概念
            </h2>
            <p class="text-lg text-gray-700 leading-relaxed">
                Redis 的持久化机制是确保数据安全的关键技术。它允许将内存中的数据保存到硬盘，即使在服务器重启后也能恢复数据。Redis 提供了两种主要的持久化方式：<span class="font-semibold text-purple-600">RDB（Redis DataBase）快照</span>和<span class="font-semibold text-purple-600">AOF（Append-Only File）日志文件</span>，每种方式都有其独特的优势和适用场景。
            </p>
        </div>

        <!-- Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 section-title">
                <i class="fas fa-project-diagram mr-3 text-gradient"></i>
                持久化机制架构图
            </h2>
            <div class="mermaid">
                graph TB
                    A[Redis 内存数据] --> B{持久化机制}
                    B --> C[RDB 快照]
                    B --> D[AOF 日志]
                    C --> E[定期快照]
                    C --> F[手动快照]
                    D --> G[命令追加]
                    D --> H[文件重写]
                    E --> I[dump.rdb]
                    F --> I
                    G --> J[appendonly.aof]
                    H --> J
                    I --> K[数据恢复]
                    J --> K
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#333,stroke-width:2px
                    style D fill:#4facfe,stroke:#333,stroke-width:2px
                    style K fill:#fa709a,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- RDB Section -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">
                <i class="fas fa-camera mr-3 text-gradient"></i>
                RDB 快照机制
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card-hover bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-gray-800">工作原理</h3>
                    <p class="text-gray-700 leading-relaxed">
                        RDB 通过创建数据的时间点快照来实现持久化。Redis 会 fork 一个子进程，将内存数据序列化后写入临时文件，完成后替换原有的 RDB 文件。这种方式能够生成紧凑的二进制文件，非常适合备份和灾难恢复。
                    </p>
                </div>
                
                <div class="card-hover bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-sliders-h"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-gray-800">配置示例</h3>
                    <div class="code-block">
                        <code>
# 900秒内至少1个键被修改<br>
save 900 1<br><br>
# 300秒内至少10个键被修改<br>
save 300 10<br><br>
# 60秒内至少10000个键被修改<br>
save 60 10000
                        </code>
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mt-8">
                <div class="bg-green-50 p-6 rounded-xl">
                    <h4 class="text-lg font-bold mb-3 text-green-800">
                        <i class="fas fa-check-circle mr-2"></i>优势
                    </h4>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-chevron-right text-green-600 mr-2"></i>文件紧凑，存储效率高</li>
                        <li><i class="fas fa-chevron-right text-green-600 mr-2"></i>恢复速度快，适合大数据集</li>
                        <li><i class="fas fa-chevron-right text-green-600 mr-2"></i>对性能影响小，fork 子进程处理</li>
                    </ul>
                </div>
                
                <div class="bg-red-50 p-6 rounded-xl">
                    <h4 class="text-lg font-bold mb-3 text-red-800">
                        <i class="fas fa-exclamation-circle mr-2"></i>局限性
                    </h4>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-chevron-right text-red-600 mr-2"></i>可能丢失最后一次快照后的数据</li>
                        <li><i class="fas fa-chevron-right text-red-600 mr-2"></i>fork 操作在大数据集时可能耗时</li>
                        <li><i class="fas fa-chevron-right text-red-600 mr-2"></i>不适合对数据完整性要求极高的场景</li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- AOF Section -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 section-title">
                <i class="fas fa-file-alt mr-3 text-gradient"></i>
                AOF 日志机制
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card-hover bg-gradient-to-br from-blue-50 to-cyan-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-pen"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-gray-800">工作原理</h3>
                    <p class="text-gray-700 leading-relaxed">
                        AOF 通过记录每个写操作命令来实现持久化。所有的写命令都会被追加到 AOF 文件末尾，恢复时通过重新执行这些命令来重建数据集。这种方式提供了更好的数据安全性保证。
                    </p>
                </div>
                
                <div class="card-hover bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-gray-800">文件重写机制</h3>
                    <p class="text-gray-700 leading-relaxed">
                        随着操作的累积，AOF 文件会不断增大。Redis 提供了重写机制，通过创建一个新的 AOF 文件，只包含重建当前数据集所需的最小命令集，从而优化文件大小和恢复速度。
                    </p>
                </div>
            </div>
            
            <div class="mt-8 bg-gradient-to-r from-indigo-50 to-purple-50 p-6 rounded-xl">
                <h4 class="text-lg font-bold mb-4 text-gray-800">
                    <i class="fas fa-cog mr-2"></i>同步策略配置
                </h4>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-white p-4 rounded-lg shadow">
                        <h5 class="font-semibold text-purple-700 mb-2">always</h5>
                        <p class="text-sm text-gray-600">每个命令都同步到磁盘，最安全但性能最低</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow">
                        <h5 class="font-